package cn.wolf.notify;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import cn.wolf.tools.Log;

/**
 * <h3>状态栏通知服务</h3>
 * <p>状态栏通知服务。启动此服务，便可用状态栏</p>
 */
public class NotificationService extends Service {

	private BroadcastReceiver notificationReceiver; // 状态栏监听

	private ExecutorService executorService;

	private TaskSubmitter taskSubmitter;

	private TaskTracker taskTracker;

	public NotificationService() {
		notificationReceiver = new NotificationReceiver();
		executorService = Executors.newSingleThreadExecutor();
		taskSubmitter = new TaskSubmitter(this);
		taskTracker = new TaskTracker(this);
	}

	@Override
	public void onCreate() {
		// telephonyManager = (TelephonyManager)
		// getSystemService(Context.TELEPHONY_SERVICE);

		taskSubmitter.submit(new Runnable() {
			public void run() {
				NotificationService.this.start();
			}
		});
	}

	@Override
	public void onStart(Intent intent, int startId) {
		Log.d("onStart()...");
	}

	@Override
	public void onDestroy() {
		Log.d("onDestroy()...");
		stop();
	}

	@Override
	public IBinder onBind(Intent intent) {
		Log.d("onBind()...");
		return null;
	}

	@Override
	public void onRebind(Intent intent) {
		Log.d("onRebind()...");
	}

	@Override
	public boolean onUnbind(Intent intent) {
		Log.d("onUnbind()...");
		return true;
	}

	public ExecutorService getExecutorService() {
		return executorService;
	}

	public TaskSubmitter getTaskSubmitter() {
		return taskSubmitter;
	}

	public TaskTracker getTaskTracker() {
		return taskTracker;
	}

	// public String getDeviceId() {
	// return deviceId;
	// }

	public void connect() {
		Log.d("connect()...");
		taskSubmitter.submit(new Runnable() {
			public void run() {
				// NotificationService.this.getXmppManager().connect();
			}
		});
	}

	public void disconnect() {
		Log.d("disconnect()...");
		taskSubmitter.submit(new Runnable() {
			public void run() {
				// NotificationService.this.getXmppManager().disconnect();
			}
		});
	}

	private void registerNotificationReceiver() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(NotificationConstants.ACTION_SHOW_NOTIFICATION);
		filter.addAction(NotificationConstants.ACTION_NOTIFICATION_CLICKED);
		filter.addAction(NotificationConstants.ACTION_NOTIFICATION_CLEARED);
		registerReceiver(notificationReceiver, filter);
	}

	private void unregisterNotificationReceiver() {
		unregisterReceiver(notificationReceiver);
	}

	// private void registerConnectivityReceiver() {
	// Log.d("registerConnectivityReceiver()...");
	// telephonyManager
	// .listen(phoneStateListener,
	// PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
	// IntentFilter filter = new IntentFilter();
	// //
	// filter.addAction(android.net.wifi.WifiManager.NETWORK_STATE_CHANGED_ACTION);
	// filter.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
	// registerReceiver(connectivityReceiver, filter);
	// }

	// private void unregisterConnectivityReceiver() {
	// Log.d("unregisterConnectivityReceiver()...");
	// telephonyManager.listen(phoneStateListener,
	// PhoneStateListener.LISTEN_NONE);
	// unregisterReceiver(connectivityReceiver);
	// }

	private void start() {
		Log.d("start()...");
		registerNotificationReceiver();
		// registerConnectivityReceiver();
		// Intent intent = getIntent();
		// startService(intent);
	}

	private void stop() {
		Log.d("stop()...");
		unregisterNotificationReceiver();
		// unregisterConnectivityReceiver();
		executorService.shutdown();
	}

	/**
	 * Class for summiting a new runnable task.
	 */
	public class TaskSubmitter {

		final NotificationService notificationService;

		public TaskSubmitter(NotificationService notificationService) {
			this.notificationService = notificationService;
		}

		@SuppressWarnings("rawtypes")
		public Future submit(Runnable task) {
			Future result = null;
			if (!notificationService.getExecutorService().isTerminated()
					&& !notificationService.getExecutorService().isShutdown() && task != null) {
				result = notificationService.getExecutorService().submit(task);
			}
			return result;
		}

	}

	/**
	 * Class for monitoring the running task count.
	 */
	public class TaskTracker {

		final NotificationService notificationService;

		public int count;

		public TaskTracker(NotificationService notificationService) {
			this.notificationService = notificationService;
			this.count = 0;
		}

		public void increase() {
			synchronized (notificationService.getTaskTracker()) {
				notificationService.getTaskTracker().count++;
				Log.d("Incremented task count to " + count);
			}
		}

		public void decrease() {
			synchronized (notificationService.getTaskTracker()) {
				notificationService.getTaskTracker().count--;
				Log.d("Decremented task count to " + count);
			}
		}

	}

}
